;(function () {
'use strict';
angular.module('services.faultLocation.appAlarmServer')
  .factory('appAlarmServer', appAlarmServer)
  .service('appAlarmServerData', AppAlarmServerData);

function appAlarmServer(Restangular, CoreService, appAlarmServerData,CoreLoading) {
  var year = new Date().getFullYear();
  var month = new Date().getMonth();
  var day = new Date().getDate();
  return {
    getPageBind: getPageBind,
    changeTime: changeTime,
    getTimeType: getTimeType,
    fmtSendData: fmtSendData,
    queryAlarmLevel: queryAlarmLevel,
    queryAlarmState: queryAlarmState,
    queryAppAlarmNum: queryAppAlarmNum,
    queryAppAlarmNumTable: queryAppAlarmNumTable,
    queryDetAlarmNums: queryDetAlarmNums,
    ypaging: ypaging
  };

  function getPageBind() {
    return {
      isStrain: '00',
      systemCode: '11',
      timeType: '1',
      startTime: new Date(year, month, day),
      endTime: new Date(year, month, day),
      msg: '',
      alarmLevel:'',
      alarmState:'',
      alarmLevelCode: '',
      alarmStateCode: ''
    };
  }

  function changeTime(timeType, sendData) {
    var year = new Date().getFullYear();
    var month = new Date().getMonth();
    var day = new Date().getDate();
    var hour = new Date().getHours();
    switch (timeType) {
      case '0':
        sendData.startTime = new Date(year, month, day, hour);
        sendData.endTime = new Date(year, month, day, hour);
        break;
      case '1':
        sendData.startTime = new Date(year, month, day);
        sendData.endTime = new Date(year, month, day);
    }
  }

  function getTimeType() {
    return [ {
      timeTypeCode: '1',
      timeTypeName: '日'
    }];
  }

  function fmtSendData(sendData) {
    var bind = {
      isStrain: '00',
      systemCode: '11',
      timeType: '1',
      startTime: new Date(year, month, day),
      endTime: new Date(year, month, day),
      msg: '',
      alarmLevelCode: [],
      alarmLevelName: [],
      alarmStateCode: [],
      alarmStateName: [],
      page: {
        currentPage: 1,
        pageSize: 10
      }
    };
    //格式化时间
    bind.startTime = CoreService.dateFormat(sendData.startTime, 'yyyy-MM-dd') + ' 00:00:00';
    /*var eTime = bind.endTime.getTime()-1;*/
    bind.endTime = CoreService.dateFormat(sendData.endTime, 'yyyy-MM-dd') + ' 23:59:59';
    if(sendData.alarmLevel!==''){
      sendData.alarmLevelCode = sendData.alarmLevel.alarmLevelCode;
      sendData.alarmLevelName = sendData.alarmLevel.alarmLevelName;
      bind.alarmLevelCode.push(sendData.alarmLevelCode);
      bind.alarmLevelName.push(sendData.alarmLevelName);
    }else{
      sendData.alarmLevelName ='全部';
    }
    if (sendData.alarmState !== '') {
      sendData.alarmStateCode = sendData.alarmState.alarmStateCode;
      sendData.alarmStateName = sendData.alarmState.alarmStateName;
      bind.alarmStateCode.push(sendData.alarmStateCode);
      bind.alarmStateName.push(sendData.alarmStateName);
    }else{
      sendData.alarmStateName='全部';
    }

    bind.timeType = sendData.timeType;
    bind.systemCode = sendData.systemCode;
    bind.msg=sendData.msg;
    return bind;
  }

  function queryAlarmLevel(params, callback) {
    Restangular.all('sas/faultLocation/appAlarms/alarmLevelQuery').post(params).then(callback);
  }
  //查询告警状态
  function queryAlarmState(params, callback) {
    Restangular.all('sas/faultLocation/appAlarms/alarmStateQuery').post(params).then(callback);
  }

  //查询应用告警数
  function queryAppAlarmNum(params) {
    Restangular.all('sas/faultLocation/appAlarms/queryAlarmNums').post(params).then(function(res) {
      CoreLoading.setState(false);
      if(res.data.data.length>0){
        res.state === '10000' ? getEchartsData(params, res.data.data) : CoreService.dialog('查询错误');
      }else{
        CoreService.dialog('查询无数据');
      }
    });
  }

  function queryAppAlarmNumTable(params, callback) {
    Restangular.all('sas/faultLocation/appAlarms/queryAlarmNums').post(params).then(callback);
  }

  function getEchartsData(send, items) {
    //debugger;
    var myEcharts = echarts.init(document.getElementById('doc'));
    var temp = {
      echartsXData: [],
      echartsSeries: [],
      title: '',
      level: '',
      state: ''
    };
    angular.forEach(items, function(item) {
      temp.level = item.alarmLevelName;
      temp.state = item.alarmStateName;
      if (temp.echartsXData.indexOf(item.time)) {
        temp.echartsXData.push(item.time);
        temp.echartsSeries.push(item.alarmNum);
      }
    });
    var title = '应用告警数统计';

    if (temp.echartsXData[0] === temp.echartsXData[temp.echartsXData.length - 1]) {
      temp.title = temp.echartsXData[0].replace(/-/g, '') + title;
    } else {
      temp.title = temp.echartsXData[0].replace(/-/g, '') + ' 到 ' + temp.echartsXData[temp.echartsXData.length - 1].replace(/-/g, '') + title;
    }
    var option = {
      title: {
        text: temp.title,
        x: 'center'
      },
      animation: false,
      dataZoom: {
        show: true,
        realtime: true
      },
      tooltip: {
        show: true
      },
      xAxis: [{
        type: 'category',
        data: temp.echartsXData
      }],
      yAxis: [{
        type: 'value'
      }],
      series: [{
        name: '告警统计分析',
        type: 'bar',
        data: temp.echartsSeries
      }]
    };
    myEcharts.setOption(option);
  }

  function queryDetAlarmNums(params) {
    Restangular.all('sas/faultLocation/appAlarms/queryDetAlarmNums').post(params).then(function(res) {
      res.state === '10000' ? appAlarmServerData.flag = res.data.data : CoreService.dialog('查询错误');
      //debugger;
      CoreLoading.setState(false);
    });

    return appAlarmServerData;
  }

  function ypaging(reqBody, resData) {

    var page = reqBody.page;
    var data = {
      totalCount: resData.total,
      lineData: []
    };
    var r= (page.currentPage - 1) * page.pageSize;
    for (var currRow = (page.currentPage - 1) * page.pageSize; currRow < resData.total && currRow < page.currentPage * page.pageSize; currRow++) {
      //console.log(currRow);
      data.lineData.push(resData.data[currRow-r]);
    }

    return data;
  }
}

function AppAlarmServerData() {
  this.flag = {};
}
})();